<template>
	<view class="nfc">
		<!--#ifndef MP-WEIXIN-->
		<text v-if="visible" class="cuIcon-NFC- text-lg" @click="startNFC"></text>
		<!--#endif-->
		<view class="cu-load load-modal" v-if="loadModal" @click="stopNFC">
			<!-- <view class="cuIcon-emojifill text-orange"></view> -->
			<text class="cuIcon-NFC- text-green radar"></text>
			<view class="gray-text tip-text slideShine">				
				 {{ value||tip }} 
			</view>
		</view>
	</view>
</template>
<script>
// #ifdef MP-WEIXIN
import NFC from '@/platforms/mp-weixin/nfc.js';
// #endif
import util from '@/common/util';
export default {
	data() {
		return {
			value: '',
			loadModal: false,
			timeout: 10,
			nfcHCE: null,
			card: null
		};
	},
	props: {
		visible: {
			type: Boolean,
			default: true
		},
		tip: {
			type: String,
			default: ''
		}
	},
	mounted() {
		// #ifdef MP-WEIXIN
		// #endif
	},
	methods: {
		startNFC() {
			this.loadModal = true;
			// #ifdef MP-WEIXIN
			var that = this;
			this.value = '';
			//cardName 汉字会乱码 AID不要超过10为长度 cardNo不要超过9位
			this.card = { _type: 0, cardName: '好老师', cardNo: '33N520888', createDate: '20180309', cardExp: '33', AID: 'F999552200' };
			this.nfcHCE = new NFC.HCE([this.card.AID], this.onOptMessageCallBack, this.onHCEMessageCallBack);
			this.nfcHCE.simple();
			// #endif
		},
		stopNFC() {
			this.loadModal = false;
			// #ifdef MP-WEIXIN
			this.nfcHCE && this.nfcHCE.stopNfcHCE();
			// #endif
		},
		/**
		 * hce操作结果相关回调
		 */
		onOptMessageCallBack(code, msg) {
			//console.log('onOptMessageCallBack');
			if (code === 0) {
				//console.log('执行成功！', msg);
			} else {
				//console.log('执行失败code=' + code + ',msg=' + msg + '\n');
				util.msg(msg);
			}
			//this.value = msg;
		},
		/**
		 * 收到读卡器发送指令
		 */
		onHCEMessageCallBack(messageType, reason, hexData) {
			var that = this;
			//console.log('onHCEMessageCallBack');
			//console.log('有读卡器读我,messageType=', messageType);
			//console.error('读卡器发来数据包:' + hexData + '\n'+hexData.toLowerCase().indexOf(that.card.AID.toLowerCase()));
			if (messageType == 1) {
				if (hexData.indexOf(that.card.AID.toLowerCase()) > 0) {
					//读卡器搜寻设备的时候发送的消息
					//console.error('读卡器发来数据包:'+ hexData)
					this.sendDataPackage();
				} else {
					//确认之后读卡器发来的消息
					var msg = NFC.tools.hexToString(hexData);
					//console.error('读卡器发来数据包2:'+ msg)
					this.$emit('onMessage', msg,that);
					//this.value = msg;
				}
			} /* else if(messageType == 2){//nfc设备离场
				 util.msg(reason) 
			} */
		},
		/**
		 * 发送数据及包
		 */
		sendDataPackage() {
			var card = this.card;
			//console.log(pad(2, 2));
			//组装TLV数据包

			/*组装卡名字*/
			var hexCardName = NFC.tools.stringToHex(card.cardName);
			hexCardName = NFC.tools.plusZero(hexCardName);

			//console.log('补零：' + hexCardName);

			//console.log('cardName=>', card.cardName, ';hexCardName=>' + hexCardName);

			//console.log('还原xCardName:', hexToString(hexCardName));

			var nameTag = 'A001';
			var len = NFC.tools.stringToHex(NFC.tools.pad(hexCardName.length / 2, 2));
			var cmdname = nameTag + len + hexCardName;
			//console.log('cmdname.TVL=>' + cmdname+"-------------------------------");

			/*组装卡号*/
			var hexCardNo = NFC.tools.stringToHex(card.cardNo);
			hexCardNo = NFC.tools.plusZero(hexCardNo);
			//console.log('cardNo=>', card.cardNo, ';hexCardNo=>' + hexCardNo);
			//console.log('还原xCardNo:',hexToString(hexCardNo));
			var noTag = 'A002';
			len = NFC.tools.stringToHex(NFC.tools.pad(hexCardNo.length / 2, 2));
			var cmdNo = noTag + len + hexCardNo;
			//console.log('cmdNo.TVL=>' + cmdNo);

			/*组装卡创建日期*/
			var hexCreateDate = NFC.tools.stringToHex(card.createDate);
			hexCreateDate = NFC.tools.plusZero(hexCreateDate);
			//console.log('hexCreateDate=>' + hexCreateDate);
			var createDateTag = 'A003';
			len = NFC.tools.stringToHex(NFC.tools.pad(hexCreateDate.length / 2, 2));
			var cmdDate = createDateTag + len + hexCreateDate;
			//console.log('cmdDate.TVL=>' + cmdDate);

			/*组装卡过期时间*/
			var hexCardExp = NFC.tools.stringToHex(card.cardExp);
			hexCardExp = NFC.tools.plusZero(hexCardExp);
			//console.log('hexCardExp=>' + hexCardExp);
			var hexCardExpTag = 'A004';
			len = NFC.tools.stringToHex(NFC.tools.pad(hexCardExp.length / 2, 2));
			var cmdExp = hexCardExpTag + len + hexCardExp;
			//console.log('cmdExp.TVL=>' + cmdExp);

			len = NFC.tools.stringToHex(((cmdname.length + cmdNo.length + cmdDate.length + cmdExp.length) / 2).toString());

			//console.log('len=' + len);

			var header = '00A40400'; //固定 不要改
			var status = '9000';
			var sendcmd = (header + len + cmdname + cmdNo + cmdDate + cmdExp + status).toUpperCase();
			//var msg = '卡片返回读卡器指令：' + sendcmd + '\n';
			//this.value = msg;
			this.nfcHCE.sendNfcHCEMessage(sendcmd);
		}
	}
};
</script>
<style>
.nfc .cu-load.load-modal [class*='cuIcon-'] {
	font-size: 120rpx;
}
.nfc .cu-load.load-modal::after {
	border-left: 6upx solid #ffcc00;
}
.nfc .tip-text{ 
	font-size: 16rpx; 
	position: absolute;top:160rpx;
}
</style>
